package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.view.View;

import java.util.Date;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.BindingListItem;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;

/**
 * Created by zxh on 16/12/14.
 */

public class CommonListItem extends BindingListItem {
    // 我上传的文章
    public static final int TYPE_ARTICLE = 0;
    // 我上传的视频
    public static final int TYPE_VIDEO = 1;
    // 我收藏的视频
    public static final int TYPE_COLLECT_VIDEO = 2;
    // 我收藏的视频
    public static final int TYPE_COLLECT_ARTICLE = 3;
    // 我的悬赏/提问
    public static final int TYPE_MY_ASK = 4;
    // 我的咨询
    public static final int TYPE_MY_ADVICE = 5;
    // 我的问题
    public static final int TYPE_MY_QUESTION = 6;
    // 我的草稿箱
    public static final int TYPE_MY_DRAFT = 7;
    //    标题title、浏览数browser、收藏数collectcoun、上传日期systime
    private int type;
    private int id;
    private String title;
    private int browser;
    private int collectcoun;
    private Date systime;
    private boolean allowDelete;
    private XListModel parent;

    public XListModel getParent() {
        return parent;
    }

    public void setParent(XListModel parent) {
        this.parent = parent;
    }

    @Bindable
    public boolean isAllowDelete() {
        return allowDelete;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Bindable
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Bindable
    public int getBrowser() {
        return browser;
    }

    public void setBrowser(int browser) {
        this.browser = browser;
    }

    @Bindable
    public int getCollectcoun() {
        return collectcoun;
    }

    public void setCollectcoun(int collectcoun) {
        this.collectcoun = collectcoun;
    }

    @Bindable
    public Date getSystime() {
        return systime;
    }

    public void setSystime(Date systime) {
        this.systime = systime;
    }

    public CommonListItem(Context context, int layoutId) {
        this(context, layoutId, true);
    }

    public CommonListItem(Context context, int layoutId, boolean allowDelete) {
        super(context, layoutId);
        this.allowDelete = allowDelete;
    }

    public void onClickItem(View view) {

        CommonFragment fragment = null;
        switch (type) {
            case TYPE_ARTICLE:
                ArticleItem item = new ArticleItem(getContext(), 0);
                item.setArticleId(id);
                item.setTitle(title);
                item.setBrowser(browser);
                ArticleDetail detail = new ArticleDetail(getContext(), item);
                fragment = new CommonFragment();
                fragment.setDataModel(detail);
                fragment.setLayoutId(R.layout.f_article_detail);
                fragment.setVariableId(BR.detail);
                HomeActivity.switchFragment((Activity) getContext(), fragment);
                break;
            case TYPE_MY_DRAFT:
                ArticleEditModel model = new ArticleEditModel(getContext());
                model.setId(id);
                model.setTitle(title);
                model.setParent(getParent());
                model.load();
                fragment = new CommonFragment();
                fragment.setDataModel(model);
                fragment.setLayoutId(R.layout.f_send_article);
                fragment.setVariableId(BR.editor);
                HomeActivity.switchFragment((Activity) getContext(), fragment);
                break;
            case TYPE_MY_ASK:
                RewardDetail rewardDetail = new RewardDetail(getContext());
                QuestionItem questionItem = new QuestionItem(getContext(), getId());
                questionItem.setId(id);
                rewardDetail.setQuestion(questionItem);
                fragment = new CommonFragment();
                fragment.setVariableId(BR.detail);
                fragment.setDataModel(rewardDetail);
                fragment.setLayoutId(R.layout.f_xuanshang_detail);
//                rewardDetail.load(1);
                HomeActivity.switchFragment((Activity) getContext(), fragment);
                break;
        }
    }

    public void onClickDelete(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean result = false;
                if (type == TYPE_ARTICLE) {
                    result = WebApi.deleteDocument(getContext(), id);
                } else if (type == TYPE_MY_DRAFT) {
                    result = WebApi.delDraft(getContext(), id);
                }
                if (result) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            HomeActivity.notifyListChange(ListBindingAdapter.ACTION_DEL, CommonListItem.this);
                        }
                    });
                }
            }
        }).start();
    }
}
